<?php
declare (strict_types=1);

namespace app\common\model;

use app\common\model\Account as AccountModel;
use app\common\tool\account\Account;
use think\Exception;
use think\facade\Db;
use think\facade\Event;

class UserSignin extends Base
{
    /**
     * 签到
     * @param $user
     * @return array
     */
    public static function add($user)
    {
        $uid = $user->id;
        if($exists = self::where('uid',$uid)->whereTime('create_time','today')->find())
            return ['code'=>-1,'msg'=>'今日已签到'];

        $yesterday = self::where(['uid'=>$uid,'cycle'=>1])->whereTime('create_time','yesterday')->value('continuous');
        $continuous = $yesterday?(int)$yesterday+1:1;
        $amount = 0;
        try{
            Db::transaction(function () use ($uid,$yesterday,$continuous,&$amount){
                if($continuous==7){
                    $amount = Config::where('name','signin_seven_currency')->value('value');
                }else{
                    $amount = Config::where('name','signin_everyday_currency')->value('value');
                }
                if(!$yesterday) self::update(['cycle'=>0],['uid'=>$uid,'cycle'=>1]);
                self::create([
                    'uid' => $uid,
                    'sign_date' => date("Y-m-d"),
                    'continuous' => $continuous,
                    'amount' => $amount
                ]);
                if($continuous == 7) self::update(['cycle'=>0],['uid'=>$uid,'cycle'=>1]);

                Account::getInstance()->basics($uid, 'currency', $amount, '签到爱贝', '+')->createNormal();

                UserNotify::create([
                    'uid' => $uid,
                    'type' => 4,
                    'title' => '签到成功',
                    'content' => "恭喜您签到成功，获得{$amount}爱贝"
                ]);
            });
        }catch (Exception $e){
            return ['code'=>-1,'msg'=>'签到失败'];
        }

        return ['code'=>1,'msg'=>'签到成功', 'data'=>['amount'=>$amount]];
    }

    /**
     * 签到周期
     * @param $user
     * @return array
     */
    public static function cycle($user)
    {
        $uid = $user->id;
        if(!$yesterday = self::where('uid',$uid)->whereTime('create_time','yesterday')->find()){
            self::where(['uid'=>$uid,'cycle'=>1])->where('create_time','<',date('Y-m-d',time()))->save(['cycle'=>0]);
        }
        if($seven = self::where('uid',$uid)->where('cycle',1)->where('continuous',7)->find()){
            self::where(['uid'=>$uid,'cycle'=>1])->save(['cycle'=>0]);
        }

        $result['continuous'] = self::where(['uid'=>$uid,'cycle'=>1])->count();
        return ['code'=>1,'msg'=>'success','data'=>$result];
    }

    public static function signInList($user, $month)
    {
        $where = ['uid'=> $user->id, 'status'=>1];
        $total_count = parent::where($where)->count();
        $continuous = parent::where($where)->where('cycle', 1)->count();
        $total_amount = parent::where($where)->sum('amount');

        $list = parent::where($where)->whereMonth('sign_date', $month)->field('uid,sign_date,create_time,status,cycle,continuous')->select();
        $data = [
            'total_count' => $total_count,
            'continuous' => $continuous,
            'total_amount' => $total_amount,
            'list' => $list
        ];
        return ['code'=>1,'msg'=>'success','data'=>$data];
    }
}
